import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public void flatten(TreeNode root) {
        if(root == null) {
            return ;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        flattem(root,queue);//前序遍历将二叉树放入到队列中
        TreeNode cur = queue.poll();
        cur.left = null;
        while(queue.isEmpty() && cur != null) {
            TreeNode prve = queue.poll();
            cur.right = prve;
            prve.left = null;
            cur = cur.right;
        }
    }
    private void flattem(TreeNode root,Queue<TreeNode> queue) {
        if(root == null) {
            return ;
        }
        queue.add(root);
        flattem(root.left,queue);
        flattem(root.right,queue);
    }
}